package com.tools.ad.adscene.inner;

import android.os.Message;

import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.hm.adsbuisiness.bean.interstitial.AMNRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.AMRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.MpRawInterstitialAd;
import com.hm.adsbuisiness.service.AdActionException;
import com.hm.adsui.card.CommonNativeAdCardView;
import com.hm.base.android.mob.AMApplication;
import com.tools.ad.AdType;
import com.tools.ad.EntranceType;
import com.tools.ad.R;
import com.tools.ad.adscene.AdReportScene;
import com.tools.ad.strategy.IStrategyExecutor;
import com.tools.ad.ui.crad.view.LargeCardView;

/**
 * desc: 杀毒清理结果页广告
 * date: 2018/4/18
 * author: ancun
 */

public class TriggerScene extends AdReportScene {

    private IStrategyExecutor mExecutor;

    public TriggerScene(AMApplication context, EntranceType entranceType) {
        super(context, entranceType);
    }

    @Override
    public boolean load(boolean forceLoad) {
        HmAdUnionPlace adUnionPlace = HmAdsSDK.getInstance().getUnionAd(mEntranceType.getPid());
        if (adUnionPlace == null) {
            return false;
        }

        mExecutor = getADModule().getADStrategyManager().getExecutorByEntranceType(mEntranceType);
        if (forceLoad || mExecutor.check(adUnionPlace)) {
            return loadUnionPlace(adUnionPlace);
        }

        return false;
    }

    @Override
    protected CommonNativeAdCardView initCardView() {
        return new LargeCardView(imContext);
    }

    @Override
    protected void onADLoaded(AdType adType) {
        Message message = Message.obtain();
        message.what = R.id.msg_ad_trigger_loaded;
        message.obj = adType;
        imContext.handleMobMessage(message);

        switch (adType) {
            case AM_INTERSTITIAL:
                if (activeAd instanceof AMRawInterstitialAd) {
                    try {
                        ((AMRawInterstitialAd) activeAd).show();
                    } catch (AdActionException e) {
                        e.printStackTrace();
                    }
                }
            case AM_NEW_INTERSTITIAL:
                if (activeAd instanceof AMNRawInterstitialAd) {
                    try {
                        ((AMNRawInterstitialAd) activeAd).show();
                    } catch (AdActionException e) {
                        e.printStackTrace();
                    }
                }
            case MP_INTERSTITIAL:
                if (activeAd instanceof MpRawInterstitialAd) {
                    try {
                        ((MpRawInterstitialAd) activeAd).show();
                    } catch (AdActionException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    @Override
    protected void onAdClose(AdType adType) {
        release();
    }

    @Override
    protected void onADClick(AdType adType) {
        super.onADClick(adType);
    }

    @Override
    protected void onADShow(AdType adType) {
        super.onADShow(adType);
        if (mExecutor != null) {
            mExecutor.record();
        }
    }

    @Override
    protected void onADLoad(AdType adType) {
        super.onADLoad(adType);
    }

    @Override
    protected void onADFail(AdType adType, int code) {
        super.onADFail(adType, code);
        imContext.handleMobEmptyMessage(R.id.msg_ad_trigger_fail);

    }
}
